﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class ScrapService : BaseService, IScrapService
    {
        private readonly IScrapDetailRepository _scrapDetailRepository;
        private readonly IUnitOfWork _unitOfWork;

        public ScrapService(IMapper mapper, IScrapRepository _scrapRepository, IScrapDetailRepository _scrapDetailRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._scrapDetailRepository = _scrapDetailRepository;
            this._unitOfWork = _unitOfWork;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddScrap(ScrapOrderDetail order, out string message)
        {
            message = "";
            ScrapOrderDetail detail = new ScrapOrderDetail();
            detail.OrderSn = order.OrderSn;
            detail.MaterialId = order.MaterialId;
            detail.MaterialName = order.MaterialName;
            detail.MaterialCode = order.MaterialCode;
            detail.Process = order.Process;
            detail.Remark = order.Remark;
            detail.Status = 1;
            _scrapDetailRepository.AddEntity(detail);
            if (!_unitOfWork.SaveChangesEntity())
            {
                return false;
            }
            return true;
        }

        public bool DelScrap(ScrapOrderDetail order)
        {
            _scrapDetailRepository.DelEntity(order);
            if (!_unitOfWork.SaveChangesEntity())
            {
                return false;
            }
            return true;
        }

        public List<ScrapOrderDetail> GetAllScrapOrders(int pageIndex, int pageSize, string? orderSn, out int totalCount)
        {
            List<ScrapOrderDetail> scrapOrderDtos = new List<ScrapOrderDetail>();
            var list = _scrapDetailRepository.GetEntityAlls();
            if (orderSn != null)
            {
                list = list.Where(x => x.OrderSn == orderSn);
            }
            var detail = _scrapDetailRepository.GetEntityAlls(list, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return detail.ToList();
        }

        public ScrapOrderDetail GetScrapOrder(long id)
        {
            return _scrapDetailRepository.QueryEntityById(id);
        }

        public bool UpdScrap(ScrapOrderDetail order)
        {
            var orderModel = _scrapDetailRepository.QueryEntityById(order.Id);
            orderModel.MaterialId = order.MaterialId;
            orderModel.MaterialCode = order.MaterialCode;
            orderModel.MaterialName = order.MaterialName;
            orderModel.Remark = order.Remark;
            orderModel.Process = order.Process;
            orderModel.SubmitPeople = order.SubmitPeople;
            _scrapDetailRepository.UpdEntity(orderModel);
            if (_unitOfWork.SaveChangesEntity())
            {
                return true;
            }
            return false;

        }
    }
}
